home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / dlpsh.c < prev    next >
C/C++ Source or Header  |  1997-08-03  |  8KB  |  386 lines

  1. /* DLP command Shell */
  2.  
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include "pi-source.h"
  6. #include "pi-socket.h"
  7. #include "pi-padp.h"
  8. #include "pi-dlp.h"
  9. #include "pi-serial.h"
  10.  
  11. #define TICKLE_INTERVAL 7
  12.  
  13.  
  14. int socket_descriptor;
  15. struct pi_socket *ticklish_pi_socket;
  16.  
  17. /* Prototypes */
  18. int user_fn(int sd, int argc, char **argv);
  19. int ls_fn(int sd, int argc, char **argv);
  20. int rm_fn(int sd, int argc, char **argv);
  21. int help_fn(int sd, int argc, char **argv);
  22. int exit_fn(int sd, int argc, char **argv);
  23. char *strtoke(char *str, char *ws, char *delim);
  24. void exit_func(void);
  25. void sigexit(int sig);
  26.  
  27. typedef int (*cmd_fn_t)(int, int, char **);
  28.  
  29. struct Command {
  30.   char *name;
  31.   cmd_fn_t func;
  32. };
  33.  
  34. struct Command command_list[] = {
  35.   { "user", user_fn },
  36.   { "ls", ls_fn },
  37.   { "rm", rm_fn },
  38.   { "help", help_fn },
  39.   { "quit", exit_fn },
  40.   { "exit", exit_fn },
  41.   { NULL, NULL }
  42. };
  43.  
  44. /* functions for builtin commands */
  45. int exit_fn(int sd, int argc, char **argv) {
  46.   exit_func();
  47.   sigexit(0);
  48.   return 0;
  49. }
  50.  
  51. int help_fn(int sd, int argc, char **argv) {
  52.   int i;
  53.  
  54.   printf("Built-in commands:\n");
  55.   for (i = 0; command_list[i].name != NULL; i++) {
  56.     printf("%s\t",command_list[i].name);
  57.     if((i%5)==4) {
  58.       printf("\n");
  59.     }
  60.   }
  61.   printf("\n");
  62.   return 0;
  63. }
  64.  
  65. int user_fn(int sd, int argc, char **argv) {
  66.   struct PilotUser U, nU;
  67.   char fl_name = 0, fl_uid = 0, fl_vid = 0, fl_pid = 0;
  68.   int c, ret;
  69.  
  70.   extern char* optarg;
  71.   extern int optind;
  72.  
  73.   optind = 0;
  74.   while ((c = getopt(argc, argv, "n:i:v:p:h")) != -1) {
  75.     switch (c) {
  76.     case 'n':
  77.       fl_name = 1;
  78.       strcpy(nU.username, optarg);
  79.       break;
  80.     case 'i':
  81.       fl_uid = 1;
  82.       nU.userID = strtoul(optarg, NULL, 16);
  83.       break;
  84.     case 'v':
  85.       fl_vid = 1;
  86.       nU.viewerID = strtoul(optarg, NULL, 16);
  87.       break;
  88.     case 'p':
  89.       fl_pid = 1;
  90.       nU.lastSyncPC = strtoul(optarg, NULL, 16);
  91.       break;
  92.     case 'h': case '?':
  93.       printf("Usage: user [ -n <username> ]\n");
  94.       printf("            [ -i <user id> ]\n");
  95.       printf("            [ -v <viewer id> ]\n");
  96.       printf("            [ -p <pc id> ]\n");
  97.       return 0;
  98.     }
  99.   }
  100.  
  101.   ret = dlp_ReadUserInfo(sd, &U);
  102.   if (ret < 0) {
  103.     printf("dlp_ReadUserInfo: err %d\n", ret);
  104.     return -1;
  105.   }
  106.  
  107.   if (fl_name + fl_uid + fl_vid + fl_pid == 0) {
  108.     printf("username = \"%s\"\n", U.username);
  109.     printf("userID = %08lx   viewerID = %08lx    PCid = %08lx\n",
  110.            U.userID, U.viewerID, U.lastSyncPC);
  111.     return 0;
  112.   }
  113.  
  114.   if (fl_name)
  115.     strcpy(U.username, nU.username);
  116.   if (fl_uid)
  117.     U.userID = nU.userID;
  118.   if (fl_vid)
  119.     U.viewerID = nU.viewerID;
  120.   if (fl_pid)
  121.     U.lastSyncPC = nU.lastSyncPC;
  122.  
  123.   U.successfulSyncDate = time(NULL);
  124.   U.lastSyncDate = U.successfulSyncDate;
  125.  
  126.   ret = dlp_WriteUserInfo(sd, &U);
  127.   if (ret < 0) {
  128.     printf("dlp_WriteUserInfo: err %d\n", ret);
  129.     return -1;
  130.   }
  131.  
  132.   return 0;
  133. }
  134.  
  135. char *
  136. timestr (time_t t)
  137. {
  138.   struct tm tm;
  139.   static char buf[50];
  140.  
  141.   tm = *localtime (&t);
  142.   sprintf (buf, "%04d-%02d-%02d %02d:%02d:%02d",
  143.        tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  144.        tm.tm_hour, tm.tm_min, tm.tm_sec);
  145.   return (buf);
  146. }
  147.  
  148. int 
  149. ls_fn(int sd, int argc, char **argv) 
  150. {
  151.   int c, ret;
  152.   int lflag = 0;
  153.   int cardno, flags, start;
  154.   int rom_flag = 0;
  155.  
  156. #ifdef sun
  157.   extern char* optarg;
  158.   extern int optind;
  159. #endif
  160.  
  161.   optind = 0;
  162.   while ((c = getopt(argc, argv, "lr")) != -1) {
  163.     switch (c) {
  164.     case 'r':
  165.       rom_flag = 1;
  166.       break;
  167.     case 'l':
  168.       lflag = 1;
  169.       break;
  170.     default:
  171.       printf("Usage: ls\n");
  172.       printf ("         -l    long format\n");
  173.       printf ("         -r    list rom instead of ram\n");
  174.       return 0;
  175.     }
  176.   }
  177.  
  178.   cardno = 0;
  179.   if (rom_flag == 0)
  180.     flags = 0x80; /* dlpReadDBListFlagRAM */
  181.   else
  182.     flags = 0x40; /* dlpReadDBListFlagROM */
  183.  
  184.   start = 0;
  185.   for (;;) {
  186.     struct DBInfo info;
  187.     long tag;
  188.  
  189.     /*
  190.      * The databases are numbered starting at 0.  The first 12 are in
  191.      * ROM, and the rest are in RAM.  The high two bits of the flags
  192.      * byte control wheter you see the ROM entries, RAM entries or both.
  193.      * start is the lowest index you want.  So, we start with 0, but
  194.      * usually we want to see ram entries, so it will return database
  195.      * number 12.  Then, we'll ask for 13, etc, until we get the NotFound
  196.      * error return.
  197.      */
  198.     ret = dlp_ReadDBList (sd, cardno, flags, start, &info);
  199.  
  200.     if (ret == -5 /* dlpRespErrNotFound */)
  201.       break;
  202.  
  203.     if (ret < 0) {
  204.       printf("dlp_ReadDBList: err %d\n", ret);
  205.       return -1;
  206.     }
  207.  
  208.     printf ("%.32s\n", info.name);
  209.     if (lflag) {
  210.       printf ("  more 0x%x; ", info.more);
  211.       printf ("flags 0x%x; ", info.flags);
  212.       tag = htonl (info.type);
  213.       printf ("type '%.4s'; ", (char *)&tag);
  214.       tag = htonl (info.creator);
  215.       printf ("creator '%.4s'; ", (char *)&tag);
  216.       printf ("version %d; ", info.version);
  217.       printf ("modnum %ld\n", info.modnum);
  218.       printf ("  cr %s; ", timestr (info.createDate));
  219.       printf ("mod %s; ", timestr (info.modifyDate));
  220.       printf ("backup %s; ", timestr (info.backupDate));
  221.       printf ("\n");
  222.     }
  223.  
  224.     if (info.index < start) {
  225.       /* avoid looping forever if we get confused */
  226.       printf ("error: index backs up\n");
  227.       break;
  228.     }
  229.  
  230.     start = info.index + 1;
  231.   }
  232.  
  233.   return 0;
  234. }
  235.  
  236. int 
  237. rm_fn(int sd, int argc, char **argv) 
  238. {
  239.   int ret;
  240.   int cardno;
  241.   char *name;
  242.  
  243.   if (argc != 2) {
  244.     printf ("Usage: rm database\n");
  245.     return (0);
  246.   }
  247.  
  248.   name = argv[1];
  249.  
  250.   cardno = 0;
  251.   if ((ret = dlp_DeleteDB (sd, cardno, name)) < 0) {
  252.     if (ret == dlpErrNotFound) {
  253.       printf ("%s: not found\n", name);
  254.       return (0);
  255.     }
  256.     printf ("%s: remove error %d\n", name, ret);
  257.     return (0);
  258.   }
  259.  
  260.   return (0);
  261. }
  262.  
  263. /* parse user commands and do the right thing.. */
  264. void handle_user_commands(int sd) {
  265.   char buf[256];
  266.   char *argv[32];
  267.   int argc;
  268.   int i;
  269.  
  270.   for (;;) {
  271.     printf("dlpsh> "); fflush(stdout);
  272.     if(fgets(buf, 256, stdin) == NULL) break;
  273.  
  274.     argc = 0;
  275.     argv[0] = strtoke(buf, " \t\n", "\"'");
  276.  
  277.     while (argv[argc] != NULL) {
  278.       argc++;
  279.       argv[argc] = strtoke(NULL, " \t\n", "\"'");
  280.     }
  281.  
  282.     if(argc == 0) continue;
  283.  
  284.     for (i = 0; command_list[i].name != NULL; i++) {
  285.       if (strcasecmp(argv[0], command_list[i].name) == 0) {
  286.         command_list[i].func(sd, argc, argv);
  287.       }
  288.     }
  289.   }
  290.   printf("\n");
  291. }
  292.  
  293. int main(int argc, char **argv) {
  294.   struct pi_sockaddr addr;
  295.   
  296.   int sd;
  297.   int ret;
  298.  
  299.   if (argc != 2) {
  300.     fprintf(stderr, "Usage: %s %s\n", argv[0],TTYPrompt);
  301.     exit(1);
  302.   }
  303.  
  304.   /* Connect to the Pilot. */
  305.  
  306.   sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP);
  307.   if (sd == -1) {
  308.     perror("pi_socket");
  309.     exit(1);
  310.   }
  311.  
  312.   addr.pi_family = PI_AF_SLP;
  313.   strcpy(addr.pi_device, argv[1]);
  314.  
  315.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  316.   if (ret == -1) {
  317.     perror("pi_bind");
  318.     exit(1);
  319.   }
  320.  
  321.   ret = pi_listen(sd, 5);
  322.   if (ret == -1) {
  323.     perror("pi_listen");
  324.     exit(1);
  325.   }
  326.  
  327.   printf("Connecting to Pilot at %s...", argv[1]); fflush(stdout);
  328.  
  329.   sd = pi_accept(sd, 0, NULL);
  330.   if (sd == -1) {
  331.     perror("pi_accept");
  332.     exit(1);
  333.   }
  334.  
  335.   printf("connected.\n");
  336.  
  337.   /* Stayin' alive, stayin' alive... */
  338.   pi_watchdog(sd, TICKLE_INTERVAL);
  339.  
  340.   socket_descriptor = sd;
  341.   atexit(exit_func);
  342.  
  343.   handle_user_commands(sd);
  344.   exit(0);
  345. }
  346.  
  347. void sigexit(int sig) {
  348.   exit(0);
  349. }
  350.  
  351. void exit_func(void) {
  352. #ifdef DEBUG
  353.   fprintf(stderr, "\n\n================== EXITING ===================\n\n\n");
  354. #endif
  355.   dlp_EndOfSync(socket_descriptor, 0);
  356.   pi_close(socket_descriptor);
  357. }
  358.  
  359. char *strtoke(char *str, char *ws, char *delim) {
  360.   static char *s, *start;
  361.   int i;
  362.  
  363.   if (str != NULL) {
  364.     s = str;
  365.   }
  366.  
  367.   i = strspn(s, ws);
  368.   s += i;
  369.   start = s;
  370.  
  371.   if (*s == '\0') {
  372.     return NULL;
  373.   } else if (strchr(delim, *s) != NULL) {
  374.     start++;
  375.     s = strchr(s+1, *s);
  376.     s[i] = '\0';
  377.     s++;
  378.   } else {
  379.     i = strcspn(s, ws);
  380.     s[i] = '\0';
  381.     s += i+1;
  382.   }
  383.  
  384.   return start;
  385. }
  386.